/*
 * 
 */

package com.memorytech.starchive.db;

import java.util.ArrayList;

import com.memorytech.starchive.entity.HistoryBarcode;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * The Class SQLiteProvider.
 */
public class SQLiteProvider {
    private SQLiteDatabase sqLiteDatabase;
    public static SQLiteProvider instance = null;
    private Context context;
    private String packageName;

    /**
     * Gets the single instance of SQLiteProvider.
     * 
     * @param context the context
     * @param packageName the package name
     * @return single instance of SQLiteProvider
     */
    public static SQLiteProvider getInstance(Context context, String packageName) {
        if (instance == null) {
            instance = new SQLiteProvider(context, packageName);
        }
        return instance;
    }

    /**
     * Instantiates a new sQ lite provider.
     * 
     * @param context the context
     * @param packageName the package name
     */
    public SQLiteProvider(Context context, String packageName) {
        this.context = context;
        this.packageName = packageName;
    }

    /**
     * Gets the list history barcode.
     * 
     * @return the list history barcode
     */
    public ArrayList<HistoryBarcode> getListHistoryBarcode() {
        ArrayList<HistoryBarcode> arrayList = new ArrayList<HistoryBarcode>();
        BarcodeReaderDBHelper databaseHelper = BarcodeReaderDBHelper.getInstance(context,
                packageName);
        try {
            sqLiteDatabase = databaseHelper.openDataBase();
            Cursor cursor = sqLiteDatabase.rawQuery("select * from barcode",
                    null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        // int id = cursor.getInt(cursor.getColumnIndex("id"));
                        String barcode = cursor.getString(cursor
                                .getColumnIndex("barcodename"));
                        byte[] image = cursor.getBlob(cursor
                                .getColumnIndex("image"));
                        HistoryBarcode historyBarcode = new HistoryBarcode();
                        historyBarcode.setBarcode(barcode);
                        historyBarcode.setImage(image);
                        arrayList.add(historyBarcode);
                    } while (cursor.moveToNext());
                }
            }
            cursor.close();
            sqLiteDatabase.close();
            databaseHelper.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    public void insertNewHistoryBarcode(HistoryBarcode historyBarcode) {
        BarcodeReaderDBHelper databaseHelper = BarcodeReaderDBHelper.getInstance(context,
                packageName);
        try {
            sqLiteDatabase = databaseHelper.openDataBase();
            ContentValues couponRecord = new ContentValues();
            couponRecord.put("barcodename", historyBarcode.getBarcode());
            couponRecord.put("image", historyBarcode.getImage());
            sqLiteDatabase.insert("barcode", null, couponRecord);
            sqLiteDatabase.close();
            databaseHelper.close();
        } catch (Exception e) {
        }
    }

    public boolean isBarcodeExisted(String string) {

        int i = 0;
        BarcodeReaderDBHelper databaseHelper = BarcodeReaderDBHelper.getInstance(context,
                packageName);
        try {
            sqLiteDatabase = databaseHelper.openDataBase();
            Cursor cursor = sqLiteDatabase.rawQuery(
                    "select  count(*) from barcode where barcodename=='"
                            + string + "'; ",
                    null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    i = cursor.getInt(0);
                }
            }
            cursor.close();
            sqLiteDatabase.close();
            databaseHelper.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return (i > 0);
    }

    /**
     * Clear db.
     */
    public void clearDB() {
        BarcodeReaderDBHelper databaseHelper = BarcodeReaderDBHelper.getInstance(context,
                packageName);
        try {
            sqLiteDatabase = databaseHelper.openDataBase();
            sqLiteDatabase.delete("barcode", null, null);
            sqLiteDatabase.close();
            databaseHelper.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
